package com.china08.yxyapi.api.home.kecheng.topic;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicCatalogRepository;

/**
 * 专题课程--列表页--二级分类名称
 * 
 * @author LiuWeidong
 *
 */

@CrossOrigin
@RestController
public class KechengTopicTwoController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsTopicCatalogRepository yxyLessonsTopicCatalogRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/topic/two/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {

        String oneId = reqModel.oneId;
        List<RespModel> returnLst = new ArrayList<>();
        RespModel aa = new RespModel();
        aa.twoId = "";
        aa.twoName = "不限";
        aa.idThree = "";
        aa.nameThree = "不限";
        returnLst.add(aa);

        // 一级为不限
        if (!StringUtils.isNotBlank(oneId)) {
            List<YxyLessonsTopicCatalog> yxyLessonsTopicCatalogLst = yxyLessonsTopicCatalogRepository.findByLevel2(2);
            for (YxyLessonsTopicCatalog yxyLessonsTopicCatalog : yxyLessonsTopicCatalogLst) {
                RespModel model = new RespModel();
                // 二级参数
                String twoId = yxyLessonsTopicCatalog.getId();
                model.twoId = yxyLessonsTopicCatalog.getId();
                model.twoName = yxyLessonsTopicCatalog.getName();

                List<YxyLessonsTopicCatalog> yxyLessonsTopicCatalogLst1 = yxyLessonsTopicCatalogRepository
                        .findDistinctNameByPidAndLevel(twoId, 3);
                for (YxyLessonsTopicCatalog yxyLessonsTopicCatalog2 : yxyLessonsTopicCatalogLst1) {
                    RespModel model2 = new RespModel();
                    model2.idThree = yxyLessonsTopicCatalog2.getId();
                    model2.nameThree = yxyLessonsTopicCatalog2.getName();
                    returnLst.add(model2);
                }

                returnLst.add(model);
            }
        }
        // 一级为非不限
        else {
            List<YxyLessonsTopicCatalog> yxyLessonsTopicCatalogLst = yxyLessonsTopicCatalogRepository
                    .findByPidAndLevel2(oneId, 2);
            for (YxyLessonsTopicCatalog yxyLessonsTopicCatalog : yxyLessonsTopicCatalogLst) {
                RespModel model = new RespModel();
                // 二级参数
                model.twoId = yxyLessonsTopicCatalog.getId();
                model.twoName = yxyLessonsTopicCatalog.getName();

                /**
                 * 三级查询出来之后, 导致二级有些名称为null
                 */
                // List<YxyLessonsTopicCatalog> yxyLessonsTopicCatalogLst1 =
                // yxyLessonsTopicCatalogRepository
                // .findDistinctNameByPidAndLevel(twoId, 3);
                // for (YxyLessonsTopicCatalog yxyLessonsTopicCatalog2 :
                // yxyLessonsTopicCatalogLst1) {
                // RespModel model2 = new RespModel();
                // model2.idThree = yxyLessonsTopicCatalog2.getId();
                // model2.nameThree = yxyLessonsTopicCatalog2.getName();
                // returnLst.add(model2);
                // }

                returnLst.add(model);
            }
        }

        return returnLst;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {

        // 一级分类id
        public String oneId;

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 二级分类ID
        public String twoId;
        // 二级分类名称
        public String twoName;
        // 三级目录ID
        public String idThree;
        // 三级目录名称
        public String nameThree;

    }

}
